/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.taskdefs;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


/**
 * <p>
 * This task is used to find the DC version using CVS. I have
 * to assume that this task is run from somewhere where a CVS
 * directory exists. This should always be true.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class CVSVersionTask extends Task {

    private String property;


    /**
     * Sets the name of the property which will contain the version.
     *
     * @param   property The property's name
     */
    public void setProperty(String property) {
        this.property = property;
    }


    /**
     * Executes the lookup of the version.
     *
     * @throws  org.apache.tools.ant.BuildException
     */
    public void execute() throws BuildException {
        File file = new File("CVS/Tag");
        if (file.exists() && file.isFile()) {

            // Read the contents of the file into a String
            StringBuffer buf = new StringBuffer(16);
            try {
                FileReader reader = new FileReader(file);
                int ch = reader.read();
                while (ch != -1) {
                    buf.append((char) ch);
                    ch = reader.read();
                }
            } catch (FileNotFoundException e) {
                throw new BuildException(e);
            } catch (IOException ioe) {
                throw new BuildException(ioe);
            }

            String tag = buf.toString().trim();
            getProject().setProperty(property, tag.substring(1));
        } else {
            getProject().setProperty(property, "0.0.0");
        }
    }
}
